package org.liaohailong.library.widget.interact.audio

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import org.liaohailong.library.R

/**
 * Author: liaohailong
 * Date: 2019/3/12
 * Time: 10:31 AM
 * Description: 音频进度条
 **/
class AudioProgressBar(context: Context, attributeSet: AttributeSet?, defStyle: Int) : View(context, attributeSet, defStyle) {
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context) : this(context, null)

    private var bufferPaint: Paint = Paint()
    private var progressPaint: Paint = Paint()
    private var barPaint: Paint = Paint()

    private var bufferColor = Color.LTGRAY
    private var progressColor = Color.WHITE
    private var barColor = Color.WHITE

    private var lineWidth = 0
    private var barRadius = 0
    private var startSpace = 0
    private var endSpace = 0

    private var progress: Float = 0.0f

    init {
        val array = context.obtainStyledAttributes(attributeSet, R.styleable.AudioProgressBar)
        bufferColor = array.getColor(R.styleable.AudioProgressBar_bufferColor, Color.LTGRAY)
        progressColor = array.getColor(R.styleable.AudioProgressBar_progressColor, Color.WHITE)
        barColor = array.getColor(R.styleable.AudioProgressBar_barColor, Color.WHITE)
        array.recycle()


        bufferPaint.isAntiAlias = true
        bufferPaint.style = Paint.Style.STROKE
        bufferPaint.color = bufferColor

        progressPaint.isAntiAlias = true
        progressPaint.style = Paint.Style.STROKE
        progressPaint.color = progressColor

        barPaint.isAntiAlias = true
        barPaint.style = Paint.Style.FILL_AND_STROKE
        barPaint.color = barColor
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val radius = h / 2f
        lineWidth = Math.floor(radius.toDouble() * 0.75f).toInt()
        barRadius = lineWidth
        startSpace = barRadius
        endSpace = startSpace
        notifySettingChanged()
    }

    fun setBufferColor(@ColorInt color: Int): AudioProgressBar {
        bufferColor = color
        return this
    }

    fun setProgressColor(@ColorInt color: Int): AudioProgressBar {
        progressColor = color
        return this
    }

    fun setBarColor(@ColorInt color: Int): AudioProgressBar {
        barColor = color
        return this
    }

    /**
     * 设置进度
     * @param progress [0.0f ~ 1.0f]
     */
    fun setProgress(progress: Float) {
        this.progress = progress
        this.progress = if (this.progress > 1) 1f else this.progress
        this.progress = if (this.progress < 0) 0f else this.progress
        invalidate()
    }

    fun notifySettingChanged() {
        bufferPaint.color = bufferColor
        progressPaint.color = progressColor
        barPaint.color = barColor

        bufferPaint.strokeWidth = lineWidth.toFloat()
        progressPaint.strokeWidth = lineWidth.toFloat()
    }

    override fun onDraw(canvas: Canvas?) {
        if (canvas == null) return
        drawBufferLine(canvas)
        drawProgressLineWithBar(canvas)
    }

    private fun drawBufferLine(canvas: Canvas) {
        val startX = startSpace
        val startY = measuredHeight / 2f
        val stopX = measuredWidth - endSpace
        canvas.drawLine(startX.toFloat(), startY, stopX.toFloat(), startY, bufferPaint)
    }

    private fun drawProgressLineWithBar(canvas: Canvas) {
        val startX = startSpace
        val startY = measuredHeight / 2f
        val currentX = startX + progress * (measuredWidth - startSpace - endSpace)
        canvas.drawLine(startX.toFloat(), startY, currentX, startY, progressPaint)
        canvas.drawCircle(currentX, startY, barRadius.toFloat(), barPaint)
    }
}